//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.cgreport.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ht.modules.system.online.cgreport.entity.OnlCgreportParam;
import com.ht.modules.system.online.cgreport.service.IOnlCgreportParamService;
import com.ht.entity.Result;
import com.ht.query.QueryGenerator;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping({"/online/cgreport/param"})
public class OnlCgreportParamController {
    private static final Logger log = LoggerFactory.getLogger(OnlCgreportParamController.class);
    @Autowired
    private IOnlCgreportParamService onlCgreportParamService;

    public OnlCgreportParamController() {
    }

    @GetMapping({"/listByHeadId"})
    public Result<?> queryPageListByHeadId(String headId) {
        QueryWrapper<OnlCgreportParam> queryWrapper = new QueryWrapper();
        queryWrapper.eq("cgrhead_id", headId);
        queryWrapper.orderByAsc(Arrays.toString(new String[]{"order_num"}));
        List<OnlCgreportParam> list = this.onlCgreportParamService.list(queryWrapper);
        Result<List<OnlCgreportParam>> result = new Result<>();
        result.setSuccess(true);
        result.setResult(list);
        return result;
    }

    @GetMapping({"/list"})
    public Result<IPage<OnlCgreportParam>> queryPageList(OnlCgreportParam onlCgreportParam, @RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<OnlCgreportParam>> result = new Result<>();
        QueryWrapper<OnlCgreportParam> queryWrapper = QueryGenerator.initQueryWrapper(onlCgreportParam, req.getParameterMap());
        Page<OnlCgreportParam> page = new Page((long)pageNo, (long)pageSize);
        IPage<OnlCgreportParam> pageList = this.onlCgreportParamService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @PostMapping({"/add"})
    public Result<OnlCgreportParam> add(@RequestBody OnlCgreportParam onlCgreportParam) {
        Result result = new Result<>();

        try {
            this.onlCgreportParamService.save(onlCgreportParam);
            result.success("添加成功！");
        } catch (Exception var4) {
            log.error(var4.getMessage(), var4);
            result.error500("操作失败");
        }

        return result;
    }

    @PutMapping({"/edit"})
    public Result<OnlCgreportParam> edit(@RequestBody OnlCgreportParam onlCgreportParam) {
        Result<OnlCgreportParam> result = new Result<>();
        OnlCgreportParam onlCgreportParamEntity = (OnlCgreportParam)this.onlCgreportParamService.getById(onlCgreportParam.getId());
        if (onlCgreportParamEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = this.onlCgreportParamService.updateById(onlCgreportParam);
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    @DeleteMapping({"/delete"})
    public Result<OnlCgreportParam> delete(@RequestParam(name = "id",required = true) String id) {
        Result<OnlCgreportParam> result = new Result<>();
        OnlCgreportParam onlCgreportParam = (OnlCgreportParam)this.onlCgreportParamService.getById(id);
        if (onlCgreportParam == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = this.onlCgreportParamService.removeById(id);
            if (ok) {
                result.success("删除成功!");
            }
        }

        return result;
    }

    @DeleteMapping({"/deleteBatch"})
    public Result<OnlCgreportParam> deleteBatch(@RequestParam(name = "ids",required = true) String ids) {
        Result<OnlCgreportParam> result = new Result<>();
        if (ids != null && !"".equals(ids.trim())) {
            this.onlCgreportParamService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        } else {
            result.error500("参数不识别！");
        }

        return result;
    }

    @GetMapping({"/queryById"})
    public Result<OnlCgreportParam> queryById(@RequestParam(name = "id",required = true) String id) {
        Result<OnlCgreportParam> result = new Result<>();
        OnlCgreportParam onlCgreportParam = (OnlCgreportParam)this.onlCgreportParamService.getById(id);
        if (onlCgreportParam == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(onlCgreportParam);
            result.setSuccess(true);
        }

        return result;
    }
}
